package com.card.manager.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.aab.service.AabService;
import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.AaaMapper;
import com.card.manager.dao.AbaMapper;
import com.card.manager.domain.Aaa;
import com.card.manager.domain.AaaExample;
import com.card.manager.domain.AaaExample.Criteria;
import com.card.manager.domain.Aba;
import com.card.manager.domain.AbaExample;
import com.card.manager.services.AaaService;

@Service("AaaServiceImpl")
public class AaaServiceImpl extends MyFrameworkSupperMapper implements
		AaaService {
	public Map<String, Object> selectAaaForPage(Aaa aaa, Aba aba,
			PageModel pageModel) {
		AaaMapper mapper = this.getMapper(AaaMapper.class);
		AaaExample aaaExample = new AaaExample();
		Criteria oredCriteria = aaaExample.createCriteria();
		com.card.manager.domain.AbaExample.Criteria oredCriteriaAba = aaaExample.aba
				.createCriteria();
		aaaExample.setPage(pageModel);
		if (StringUtils.isNotEmpty(pageModel.getSort()))
			aaaExample.setOrderByClause(pageModel.getSort() + " "
					+ pageModel.getOrder());
		if (StringUtils.isNotEmpty(aaa.getUsername()))
			oredCriteria.andUsernameEqualTo(aaa.getUsername());
		if (StringUtils.isNotEmpty(aaa.getOwnercard()))
			oredCriteria.andOwnercardEqualTo(aaa.getOwnercard());
		if (StringUtils.isNotEmpty(aaa.getTelephone()))
			oredCriteria.andTelephoneEqualTo(aaa.getTelephone());
		if (StringUtils.isNotEmpty(aba.getAbaid()))
			oredCriteriaAba.andAbaidEqualTo(aba.getAbaid());
		//
		oredCriteria.andDelflgEqualTo((byte) 1);

		List<Aaa> list = mapper.selectByAaa_Aba(aaaExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("total", pageModel.getTotalCount());
		modelMap.put("rows", list);
		return modelMap;
	}

	// public List selectAaaAba() {
	// AaaMapper mapper = this.getMapper(AaaMapper.class);
	// AaaExample aaaExample = new AaaExample();
	// //aaaExample.createCriteria().andUsernameIsNotNull();
	// //aaaExample.createCriteria().andUsernameEqualTo(aaa.getUsername());
	// // aaaExample.createCriteria().andDelflgEqualTo((byte)1);
	// aaaExample.createCriteria().andDelflgEqualTo((byte)1);
	// List<Aaa> list = mapper.selectByAaa_Aba(aaaExample);
	// return list;
	// }
	@Transactional
	@Override
	public void deleteAaas(final List<Aaa> aaa)
			throws ModifiedByAnotherUserException, Exception {
		final AbaMapper abamapper = this.getMapper(AbaMapper.class);
		batchSubmit(AaaMapper.class, new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if (aaa != null && aaa.size() > 0) {
					for (Aaa a : aaa) {
						Aba aba = new Aba();
						aba.setDelflg((byte) 0);
						AbaExample examper = new AbaExample();
						examper.createCriteria().andUseridEqualTo(a.getId());
						abamapper.updateByExampleSelective(aba, examper);
						a.setDelflg((byte) 0);
						((com.card.manager.dao.AaaMapper) mapper)
								.updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}

	//
	// @Transactional
	// public void addOrUpdate(Aaa aaa) {
	// // aaa = new Aaa();
	// Aaa bbb = new Aaa();
	// bbb.setUsername("bbb");
	// AaaMapper mapperb = this.getMapper(AaaMapper.class);
	// mapperb.insert(bbb);
	// AaaMapper mapper = this.getMapper(AaaMapper.class);
	// mapper.insert(aaa);
	// }

	@Override
	public void addOrUpdate(Aaa aaa) {
		AaaMapper mapper = this.getMapper(AaaMapper.class);
		// Aaa queryResult = mapper.selectByPrimaryKey(aaa.getUserid());
		if (aaa.getId() == null) {
			mapper.insert(aaa);
		} else {
			AaaExample aaaExample = new AaaExample();
			aaaExample.createCriteria().andIdEqualTo(aaa.getId());
			mapper.updateByExample(aaa, aaaExample);
		}
	}
}